package com.refusea.train.mapper

import com.refusea.train.domain.entity.LessonConfirmed
import com.refusea.train.domain.req.LessonHistoryReq
import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Options
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update

@Mapper
interface LessonConfirmedMapper {

    @Insert(
        """
        INSERT INTO lesson_confirmed (
            school_id, course_id, student_id, teacher_id, lesson_id, 
            class_group_name, classroom_name, lesson_date, start_time, end_time, teacher_salary
        ) VALUES (
            #{schoolId}, #{courseId}, #{studentId}, #{teacherId}, #{lessonId}, 
            #{classGroupName}, #{classroomName}, #{lessonDate}, #{startTime}, #{endTime}, #{teacherSalary}
        )
    """
    )
    @Options(useGeneratedKeys = true, keyProperty = "id")
    fun insert(lessonCompleted: LessonConfirmed): Int

    @Delete("DELETE FROM lesson_confirmed WHERE id = #{id}")
    fun delete(id: Int): Int

    @Update(
        """
        UPDATE lesson_confirmed SET 
            school_id = #{schoolId}, course_id = #{courseId}, student_id = #{studentId}, 
            teacher_id = #{teacherId}, lesson_id = #{lessonId}, class_group_name = #{classGroupName}, 
            classroom_name = #{classroomName}, lesson_date = #{lessonDate}, start_time = #{startTime}, 
            end_time = #{endTime}, teacher_salary = #{teacherSalary}
        WHERE id = #{id}
    """
    )
    fun update(lessonCompleted: LessonConfirmed): Int

    @Select("SELECT * FROM lesson_confirmed WHERE id = #{id}")
    fun select(id: Int): LessonConfirmed?

    @Select("SELECT * FROM lesson_confirmed")
    fun list(): List<LessonConfirmed>

    @Insert(
        """
        <script>            
        INSERT INTO lesson_confirmed (school_id, course_id, teacher_id, class_group_name, 
        classroom_name, lesson_number, lesson_time, teacher_salary, students) VALUES
        <foreach collection="confirmedList" item="e" separator=",">
            (#{e.schoolId}, #{e.courseId}, #{e.teacherId}, #{e.classGroupName}, #{e.classroomName}, 
            #{e.lessonNumber}, #{e.lessonTime}, #{e.teacherSalary}, #{e.students})                    
        </foreach>            
        </script>        
        """
    )
    fun inserts(confirmedList: List<LessonConfirmed>): Int


    @Select("<script>SELECT count(*) FROM lesson_confirmed WHERE ${ORDER_CONDITION}</script>")
    fun count(req: LessonHistoryReq): Int

    @Select("<script>SELECT * FROM lesson_confirmed WHERE $ORDER_CONDITION order by id desc limit #{offset}, #{limit}</script>")
    fun query(req: LessonHistoryReq): List<LessonConfirmed>

    companion object {
        const val ORDER_CONDITION = """
        school_id = #{schoolId}
        <if test='start!=null'>and lesson_time >= #{start}</if>
        <if test='end!=null'>and lesson_time &lt; #{end}</if>
        <if test='teacherId!=null and teacherId!=0'>and teacher_id= #{teacherId}</if>
        """
    }
}
